from etgui import *
from PageLayout import *
from PrefsMirrorEntries import *
from AllModelDictEditors import *
from PrefsMirrorEntries import *

class CrispParserInfoDictEditor(LayeredModelDictEditor):
	__super=LayeredModelDictEditor
	def NewValue(self):
		return {
			"__frame__":"CrispParserInfo",
			"Name":"",
			"ToolProgram":self.GetCurrentToolProgram(),
		}
	def IsUniqueName(self,name):
		items=EnvGetDict(self.path)
		if not items:
			return 1
		return not items.has_key(name)
	DoEdit=None
	DoNew=None
	def DoMakePreview(self,model):
		return MakeCompilerSettings(model)
	def GetCurrentToolProgram(self):
		tp= self.model.GetValue("Crisp.ToolProgram")
		if not tp:
			tp="Gcc2ToolProgram"
		return tp
	def GetIndex(self):
		# get all values
		names=self.__super.GetIndex(self)
		items=EnvGetDict(self.path)
		if not items: # items might ne None
			items={}
		index=[]
		# and filter out the not relevant ones
		toolprogram=self.GetCurrentToolProgram()
		for name,frame in items.items():
			if name in names:
				tp=frame.get('ToolProgram',None)
				if tp is None:
					tp="Gcc2ToolProgram"
				if tp==toolprogram:
					index.append(name)
		index.sort()
		return index
	def ToolProgramChanged(self,model,slot):
		"""Called when the poup changes"""
		self.Reload()
def MakeCompilerSettings(model,popup=None):
		if popup:
			popup=["Compiler",popup]
		else:
			popup=[]
		crispPage=TabPage(
			"Crisp Parser",   # page selector
			MakeSectionLayout(
				Form(
					items=popup
					+MakeSplitIncludeListEditor(model,"IncludeDirs","Include Directive(s)","-I")
					+MakeSplitStringListEditor(model,"Macros","Preprocessor Directive(s)","-D")
					+MakeSplitStringListEditor(model,"ParserArgs","Parser Argument(s)","")
				)
			)
		)

		buildPage=TabPage(
			"Build Trace",   # page selector
			MakeSectionLayout(
				Form(
					items=[
						"Compiler Location",PotGuiFileSelector("Path",gTextFieldLen),
						"Compiler Name",PotTextField("BaseName"),
					]
				)
			)
		)
		return MakeTabSheetLayout(crispPage,buildPage)

def MakeToolCategoryPopup(model,crispdicteditor):
	items= (
		("GNU C/C++ Compiler", 			"Gcc2ToolProgram"),
		("SUN CC Compiler", 			"SunCC4ToolProgram"),
		("Microsoft Developer Studio",  "MicrosoftToolProgram"),
		("Archiver", 					"ArchiverToolProgram"),
	)
	popup=PotGuiPopupButton("Crisp.ToolProgram")
	model.AddModelObserver(crispdicteditor.ToolProgramChanged,"Crisp.ToolProgram")
	popup.RemoveAllItems()
	for title,value in items:
		popup.AddItem(title,value)   # (display, model)
	return popup


def MakePageCrispParsing(model):
	crisponsave=AlignBox(
		eVObjHExpand,
		inner=PotGuiToggleButton(
			"Crisp.UseCrispOnSave",
			"Crisp File Reparse on Save"
		)
	)
	FieldEnabler(model,"Crisp.VisibleInGUI",
		items=[
			crisponsave
		]
	)
	crispdicteditor=CrispParserInfoDictEditor(model,"CrispParserInfo","CrispParser")
	parserInfo=MakeModelDictEditorLayout(crispdicteditor)
	toolcategory=MakeToolCategoryPopup(model,crispdicteditor)
	return MakePageLayout(eVObjHExpand|eVObjVExpand,
		MakeBorderedSectionLayout(
			"General",
			crisponsave
		),
		MakeBorderedSectionLayoutWithAlign(
			"Tools settings for crisp parsing",
			align=eVObjHExpand|eVObjVExpand,
			items=[
				Form(
					items=[
						"Tool Category",toolcategory,
					]
				),
				parserInfo
			]
		)
	)
	

